var index = angular.module('index',[]);


index.controller('posts',function($scope,$http){
    $scope.isShow = false;
    $scope.hotPosts = [];
    $scope.posts = [];
    $scope.comments = [];
    $scope.hotNum = 0;
    $scope.next = "next";
    $scope.prev = "previous";
    //换页部分变量声明等
    $scope.startNum = 0;//确定从数据库中第几个开始取find().skip(startNum)
    $scope.itemCount = 5;//每一页的文章数量find().limit(itemCount)
    $scope.lastSNum = 0;
    //确定iframe是否显示
    $scope.showCoverIframe = false;
    $scope.endNum = $scope.startNum+$scope.itemCount;
    $scope.fnFilterHot = function(e){
        if(e["commentNum"]>=40){
            $scope.hotNum++;
            return e;
        }
    };
    //从后台获取相应页的文章数据
    $scope.getPage = function(startN,itemC){
        $http({
            url:"/getPage",
            method:"post",
            data:{startNum:startN,itemCount:itemC}
        }).success(function (data) {
            var posts = data['posts'];
            if(posts.length){
                $scope.posts = posts;
                //获取每篇文章评论数目
                $http({
                    url:'/getCommentsNumForAId',
                    method:'get'
                }).success(function(data){
                    $scope.commentsNumForAId = {};
                    var commentsNum = data['commentsNum'];
                    for(var i=0;i<commentsNum.length;i++){
                        $scope.commentsNumForAId[commentsNum[i]['_id']] = commentsNum[i]['num'];
                    }
                    if($scope.startNum>0){
                        document.querySelector('#prev').classList.remove('disabled');
                    }else{
                        document.querySelector('#prev').classList.add('disabled');
                    }
                    $scope.isShow = true;
                });
            }else{
                $scope.startNum = $scope.lastSNum;
                document.querySelector('#next').classList.add('disabled');
                $scope.isShow = true;
                return 0;
            }

        }).error(function(data){
            console.log(data);
            $scope.isShow = true;
        });
    };
    //换页函数
    $scope.changePage = function(mode){
        $scope.lastSNum = $scope.startNum;
        if(mode==='next'){
            $scope.startNum += $scope.itemCount;
        }else if(mode==='prev'&& $scope.startNum>0){
            $scope.startNum -= $scope.itemCount;
            document.querySelector('#next').classList.remove('disabled');
        }
        var result = $scope.getPage($scope.startNum,$scope.itemCount);
        if(!result){
            alert('这是最后一页辣');
        }
    };





    //第一次打开页面获取前三个博文
    $scope.getPage($scope.startNum,$scope.itemCount);



    //点击阅读全文后不跳转弹出iframe
    $scope.readInner = function(url){
        var iframe = document.getElementById('iframe-main');

        $scope.showCoverIframe = true;
        document.getElementById('cover-iframe').classList.remove('fadeOutDown');
        document.getElementById('cover-iframe').classList.add('fadeInUp');

        setTimeout(function(){
            iframe.src = url;
        },700);
    };
    $scope.closeIframe = function(){
        var iframe = document.getElementById('iframe-main');
        document.getElementById('cover-iframe').classList.remove('fadeInUp');
        document.getElementById('cover-iframe').classList.add('fadeOutDown');
        iframe.src = "";
    }
});




